package com.ec.system.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.system.modules.sys.dto.SysRoleMenuDTO;
import com.ec.system.modules.sys.entity.SysRoleMenu;
import com.ec.system.modules.sys.service.ISysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单
 * </p >
 *
 * @author Eric Lee
 * @since 2022-05-08 16:05
 */
@RestController
@RequestMapping("/role-menu")
public class SysRoleMenuController {

    @Autowired
    private ISysRoleMenuService roleMenuService;

    @PostMapping
    public Result<Void> addRoleMenus(@RequestBody SysRoleMenuDTO sysRoleMenuDTO) {
        List<SysRoleMenu> list = new ArrayList<>();
        sysRoleMenuDTO.getMenuIds().forEach(y -> {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(y);
            roleMenu.setRoleId(sysRoleMenuDTO.getRoleId());
            list.add(roleMenu);
        });
        return ResponseMsg.status(roleMenuService.saveBatch(list));
    }

    @PutMapping
    public Result<Void> modifyRoleMenus(@RequestBody SysRoleMenuDTO sysRoleMenuDTO) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId,sysRoleMenuDTO.getRoleId());
        boolean remove = roleMenuService.remove(wrapper);
        if(remove){
            return addRoleMenus(sysRoleMenuDTO);
        }
        return ResponseMsg.error();
    }
}